﻿namespace EasyToolkit.Tools.Log
{
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.Configuration;
    using System.Reflection;

    public sealed class Logger
    {
        #region attributes and properties

        [Import]
        private LoggerManager LoggerManager = null;

        private static volatile Logger instance;
        private static object syncRoot = new object();

        public static ILogger Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new Logger();
                            AggregateCatalog aggregateCatalog = new AggregateCatalog();
                            aggregateCatalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

                            string logAssemblyName = ConfigurationManager.AppSettings["EasyToolkitLog"];
                            //if (! string.IsNullOrEmpty(logAssemblyName))
                            //{
                            //    Assembly assembly = Assembly.Load(logAssemblyName);
                            //    aggregateCatalog.Catalogs.Add(new AssemblyCatalog(assembly));
                            //}
                            CompositionContainer container = new CompositionContainer(aggregateCatalog);
                            container.ComposeParts(instance);
                        } 
                   }
                }
                return instance.LoggerManager.Instance;
            }
        }
        #endregion

        private Logger()
        {}
    }
}
